package com.jiandaoshou;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.jiandaoshou.adapter.MyYinHangAdapter;
import com.jiandaoshou.base.BaseActivity;
import com.jiandaoshou.entry.CardInfo;
import com.jiandaoshou.http.APIDefine;
import com.jiandaoshou.http.Http;
import com.jiandaoshou.utils.DensityUtil;
import com.jiandaoshou.utils.JsonUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyYinHangList extends BaseActivity {
    private String shopId;
    private MyYinHangAdapter adapter;
    private ListView listView;
    private List<MyYinHangAdapter.Choice> datas = new ArrayList<>();
    private XRefreshView xRefreshView;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    datas = (List<MyYinHangAdapter.Choice>) msg.obj;
                    adapter = new MyYinHangAdapter(MyYinHangList.this);
                    adapter.addAll(datas);
                    listView.setAdapter(adapter);
                    break;
            }
        }
    };

    @Override
    public boolean showBackLogo() {
        return true;
    }

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_choiceyinhangka;
    }

    @Override
    protected String getTitleText() {
        return "我的银行卡";
    }

    @Override
    public void init() {
        xRefreshView = $(R.id.refresh);
        //设置xRefreshView
        xRefreshView.setHideFooterWhenComplete(false);
        xRefreshView.setMoveForHorizontal(true);
        xRefreshView.setAutoLoadMore(true);
        xRefreshView.setPullLoadEnable(true);
        listView = $(R.id.yinhangkalist);

        shopId = getIntent().getStringExtra("shopId");

        addFootView();
        //加载数据
        GetData(0);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                adapter.update(datas.get(position), position);
                Intent intent = new Intent(MyYinHangList.this, TiXianAct.class);
                intent.putExtra("bankname", datas.get(position).bank.getStrBankName());
                intent.putExtra("bank_id", datas.get(position).bank.getStrID());
                intent.putExtra("cardnum", datas.get(position).bank.getStrCardNumber());
                setResult(1, intent);
                finish();
            }
        });

        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh() {
                super.onRefresh();
                datas.clear();
                GetData(1);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                handler.sendEmptyMessageDelayed(0,2000);
            }
        });

    }

    private void GetData(final int type) {
        HashMap<String, String> map = new HashMap<>();

        map.put("strShopID", shopId);
        Http.post(APIDefine.TF_Interface_GetUserCardInfo, map, new Http.HttpCallBack() {
            @Override
            public void onHttpSuccess(String response) throws Exception {
                JSONObject object = new JSONObject(response);
                JSONArray array = object.getJSONArray("CardInfoList");
                CardInfo info = null;
                MyYinHangAdapter.Choice choice = null;
                for (int i = 0; i < array.length(); i++) {
                    info = JsonUtil.parser.fromJson(array.get(i).toString(), CardInfo.class);
                    choice = new MyYinHangAdapter.Choice();
                    choice.bank = info;
                    datas.add(choice);
                }

                if(type==1){
                    xRefreshView.stopRefresh();
                }else{
                    xRefreshView.stopLoadMore();
                }

                Message msg = handler.obtainMessage();
                msg.what = 0;
                msg.obj = datas;
                msg.sendToTarget();

            }
        });
    }

    private void addFootView() {
        TextView textView = new TextView(this);
        textView.setId(android.R.id.text1);
        int pading = DensityUtil.dip2px(this, 5);
        textView.setPadding(pading, pading, pading, pading);
        textView.setText("添加新卡提现");
        listView.addFooterView(textView);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MyYinHangList.this, YinHangKaDetailAct.class);
                intent.putExtra("shopId",shopId);
                startActivityForResult(intent,222);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==222) {
            if (resultCode==RESULT_OK) {
                datas.clear();
                GetData(0);
            }
        }
    }


}
